/**
 * 收割者，无法移动
 */

let baser = require("./baser");

// 返回baser的内部生命函数
module.exports = function () {
    return baser(
        /**
         * 准备函数，初始化缓存，发布任务
         * @param {Creep} creep
         */
        function (creep) {
            // 初始化相关缓存
            // 获取能量矿点
            let sources = creep.room.find(FIND_SOURCES);
            sources.some(function (source) {
                // 获取旁边的容器
                let container = source.pos.findClosestByRange(FIND_STRUCTURES, {
                    filter: (structure) => structure.structureType == STRUCTURE_CONTAINER &&
                        structure.pos.isNearTo(source)
                });
                // 有容器并且上面没有本类采集者
                if (container) {
                    let same = container.pos.lookFor(LOOK_CREEPS)[0];
                    if (!same || same.memory.role != creep.memory.role) {
                        creep.memory.pos = container.pos;
                        creep.memory.containerid = container.id;
                        return true;
                    }
                    else {
                        // 是自己
                        if (creep == same) {
                            creep.memory.pos = container.pos;
                            creep.memory.containerid = container.id;
                            return true;
                        }
                    }
                }
            });
            if (!creep.room.memory.pulltasks)
                creep.room.memory.pulltasks = [];
            if (!creep.room.memory.haspulltasks) {
                creep.room.memory.haspulltasks = {};
            }
            // 发布任务
            if (!creep.pos.isEqualTo(creep.memory.pos.x, creep.memory.pos.y) &&
                !creep.room.memory.haspulltasks[creep.id]) {
                creep.say('发布');
                creep.room.memory.pulltasks.splice(creep.room.memory.pulltasks.length, 0, creep.id);
                creep.room.memory.haspulltasks[creep.id] = true;
            }
            return 3;
        },
        /**
         * 工作函数，采集
         * @param {Creep} creep
         */
        function (creep) {
            if (creep.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => structure.structureType == STRUCTURE_CONTAINER
            }).store[RESOURCE_ENERGY] > 1990) {
                return 3;
            }
            creep.actionHarvest(creep.pos);
            return 2;
        },
        /**
         * 结束函数，闲逛或其他
         * @param {Creep} creep
         */
        function (creep) {
            if (!creep.memory.pos) {
                return 1;
            }
            // 是否在指定位置
            if (creep.pos.isEqualTo(creep.memory.pos.x, creep.memory.pos.y)) {
                return 2;
            }
            // 是否已发布任务
            if (!creep.room.memory.haspulltasks[creep.id]) {
                return 1;
            }
            // 位置上是否有本类采集者
            let same = Game.getObjectById(creep.memory.containerid).pos.lookFor(LOOK_CREEPS)[0];
            if (same && same.memory.role == creep.memory.role) {
                creep.say('有同类');
                return 1;
            }
            return 3;
        });
}